package com.acme.bankapp.service;

import com.acme.bankapp.domain.Bank;
import com.acme.bankapp.domain.Client;
import com.acme.bankapp.domain.Gender;
import com.acme.bankapp.domain.accounts.Account;
import com.acme.bankapp.domain.accounts.CheckingAccount;
import com.acme.bankapp.domain.accounts.SavingAccount;

import java.io.*;
import java.util.StringTokenizer;

/**
 * Created by Java Student on 11/19/2014.
 */
public class BankDataLoaderService {
    public static void saveBank(Bank bank, String fileName) throws IOException {
        saveBank(bank, new FileOutputStream(fileName));
    }

    public static Bank loadBank(String fileName) throws IOException {
        return loadBank(new FileInputStream(fileName));
    }

    public static void saveBank(Bank bank, OutputStream os) throws IOException {
        BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(os) );

        for (Client client : bank.getClients()) {
            Account account = client.getAccount();

            StringBuilder line = new StringBuilder();
            line.append("accounttype=");

            if (account instanceof SavingAccount){
                line.append("s;");
            } else if (account instanceof CheckingAccount){
                line.append("c;");
            }

            double overdraft = account.maximumAmountToWithdraw() - account.getBalance();
            line
                    .append("balance=").append(account.getBalance())
                    .append(";overdraft=").append(overdraft)
                    .append(";name=").append(client.getName())
                    .append(";gender=").append(client.getSex().toString().toLowerCase().charAt(0))
                    .append(";");

            bw.write(line.toString());
            bw.newLine();
            bw.flush();
        }
    }

    public static Bank loadBank(InputStream is) throws IOException {
        BufferedReader br = new BufferedReader( new InputStreamReader(is));
        String line;
        Bank bank = new Bank();

        while( (line = br.readLine()) != null ){
            StringTokenizer st = new StringTokenizer(line,";");
            String accType = st.nextToken().split("=")[1];
            double balance = Double.parseDouble(st.nextToken().split("=")[1]);
            double overdraft = Double.parseDouble(st.nextToken().split("=")[1]);
            String name = st.nextToken().split("=")[1];
            String g = st.nextToken().split("=")[1];

            Gender gender = null;
            switch (g){
                case "m":
                    gender = Gender.MALE;
                    break;
                case "f":
                    gender = Gender.FEMALE;
                    break;
            }

            Account account = null;
            switch (accType){
                case "c":
                    account = new CheckingAccount(balance, overdraft);
                    break;
                case "s":
                    account = new SavingAccount(balance);
                    break;
            }

            Client client = new Client(name, gender, account);

            BankService.addClient(bank, client);
        }

        return bank;
    }
}
